/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.deployment.helper;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Node;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.deployment.Activator;
import com.cea.papyrus.diagram.deployment.preference.DeploymentDiagramPreferenceConstants;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Defines management of nodes.
 * 
 * @author Gilles Cannenterre
 * @since 15 janv. 07
 */
public class NodeNodeHelper extends NodeHelper {

	/**
	 * The channel.
	 */
	private int channel = 203;

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#canCreateGraphElement(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){
		if (parent instanceof Diagram) {
			return true;
		} else if (parent.getSemanticModel()!=null){
			Element parentElt= ((Uml1SemanticModelBridge)parent.getSemanticModel()).getElement();
			if(parentElt!=null){
				return canCreateUMLElement(parentElt);
			}
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	@Override
	public  Element createUMLElement(String name, Element umlParent){
		Debug.debug(this,"createUMLElement(String,Element)", channel); 
		Node aNode = null;
		if (umlParent instanceof Node) {
			aNode = UMLFactory.eINSTANCE.createNode();
			aNode.setName(name);
			((Node) umlParent).getNestedNodes().add(aNode);			
		} else if (umlParent instanceof Package) {
			aNode = UMLFactory.eINSTANCE.createNode();
			aNode.setName(name);
			((Package) umlParent).getPackagedElements().add(aNode);	
		} else {
			aNode = UMLFactory.eINSTANCE.createNode();
			aNode.setName(name);
			((Package)umlParent.getNearestPackage()).getPackagedElements().add(aNode);
		}
		return aNode;		
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#canCreateUMLElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if ( (umlParent instanceof Package) || (umlParent instanceof Node) ) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#addUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().add((PackageableElement)element);
		}
		if (umlParent instanceof Node) {
			return ((Node)umlParent).getNestedNodes().add((Node)element);		
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#removeUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().remove(element);
		}
		if (umlParent instanceof Node) {
			return ((Node)umlParent).getNestedNodes().remove(element);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#getBaseString()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public String getBaseString() {
		return "Node_"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#getNewUMLElementName(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public String getNewUMLElementName(Element umlParent) {
		String name = ""; //$NON-NLS-1$
	
		// i <10000: avoid infinite loops
		for(int i=0; i< 10001; i++ ) {
			name = getBaseString()+i;
			if (umlParent instanceof Package){
				if(null == ((Package)umlParent).getPackagedElement(name)) {
					return name;
				}
			} else if (umlParent instanceof Node){
				if(null == ((Node)umlParent).getNestedNode(name)) {
					return name;
				}
			} else {
				if(null == ((Package)umlParent.getNearestPackage()).getPackagedElement(name)) {
					return name;
				}
			}
		}
		return getBaseString()+"X"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#setGraphElementPreferences(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	/**
	 * 
	 * 
	 * @param graphElement 
	 */
	@Override
	public void setGraphElementPreferences(GraphElement graphElement) {	
		// Get the preference store
		IPreferenceStore store = Activator.getDefault().getPreferenceStore();
		
		// Set colors and font
		graphElement.setBackgroundColor(DeploymentDiagramPreferenceConstants.getPreferenceNodeBackgroundColor(store));
		graphElement.setBorderColor(DeploymentDiagramPreferenceConstants.getPreferenceNodeBorderColor(store));
		graphElement.setFontColor(DeploymentDiagramPreferenceConstants.getPreferenceNodeFontColor(store));
		FontData[] fontdata = DeploymentDiagramPreferenceConstants.getPreferenceNodeFont(store);
		if (fontdata.length>0){
			graphElement.setFontFamily(fontdata[0].getName());
			graphElement.setFontSize(fontdata[0].getHeight());
		}
	}

	/**
	 * Gets the graph node size.
	 * 
	 * @return the graph node size
	 */
	public Dimension getGraphNodeSize() {
		return new Dimension(100,100);
	}
}